﻿// Copyright 2009 Mike Geise
// 
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// 
//     http://www.apache.org/licenses/LICENSE-2.0
// 
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

namespace Falcon.Core.Services
{
	using System.Collections.Generic;
	using Falcon.Core.Collections;
	using Falcon.Core.Domain;
	using Falcon.Core.Domain.Repositories;
	using Falcon.Core.Domain.Services;
	using Falcon.Core.Domain.Values;

	public class SettingService : ISettingService
	{
		/// <summary>
		/// Initializes a new instance of the <see cref="SettingService"/> class.
		/// </summary>
		/// <param name="settingRepository">The setting repository.</param>
		public SettingService(ISettingRepository settingRepository)
		{
			this.SettingRepository = settingRepository;
		}

		/// <summary>
		/// Gets or sets the setting repository.
		/// </summary>
		/// <value>The setting repository.</value>
		public ISettingRepository SettingRepository
		{
			get; 
			private set;
		}

		/// <summary>
		/// Gets the setting by the specified specification.
		/// </summary>
		/// <param name="specification">The <see cref="SettingSpecification"/>.</param>
		/// <returns></returns>
		public Setting GetBy(SettingSpecification specification)
		{
			return this.SettingRepository.GetBy(specification);
		}

		/// <summary>
		/// Gets the setting by id.
		/// </summary>
		/// <param name="id">The id.</param>
		/// <returns></returns>
		public Setting GetById(int id)
		{
			return this.SettingRepository.GetById(id);
		}

		/// <summary>
		/// Gets all the settings.
		/// </summary>
		/// <returns></returns>
		public IList<Setting> GetAll()
		{
			return this.SettingRepository.GetAll();
		}

		/// <summary>
		/// Gets all the settings by the specified specification.
		/// </summary>
		/// <param name="specification">The <see cref="SettingSpecification"/>.</param>
		/// <returns></returns>
		public IPagedList<Setting, SettingSpecification> GetPaged(SettingSpecification specification)
		{
			return this.SettingRepository.GetPaged(specification);
		}

		/// <summary>
		/// Creates the setting.
		/// </summary>
		/// <returns></returns>
		public Setting Create()
		{
			return new Setting();
		}

		/// <summary>
		/// Initializes this instance.
		/// </summary>
		public void Init()
		{
			var values = this.GetAll();
			var settings = new Settings();

			settings.Init(values);
		}

		/// <summary>
		/// Rebuilds this instance.
		/// </summary>
		public void Rebuild()
		{
			var values = this.GetAll();
			var settings = new Settings();

			settings.Rebuild(values);
		}

		/// <summary>
		/// Inserts the specified setting.
		/// </summary>
		/// <param name="setting">The <see cref="Setting"/>.</param>
		public void Insert(Setting setting)
		{
			this.SettingRepository.Insert(setting);
		}

		/// <summary>
		/// Inserts the specified setting.
		/// </summary>
		/// <param name="setting">The setting.</param>
		/// <param name="value">The value.</param>
		public void Insert(Setting setting, SettingCreate value)
		{
			value.Validate();

			setting.Title = value.Title;
			setting.Group = value.Group;
			setting.Key = value.ItemKey;
			setting.Value = value.ItemValue;

			this.Insert(setting);
		}

		/// <summary>
		/// Updates the specified setting.
		/// </summary>
		/// <param name="setting">The <see cref="Setting"/>.</param>
		public void Update(Setting setting)
		{
			this.SettingRepository.Update(setting);
		}

		/// <summary>
		/// Updates the specified setting.
		/// </summary>
		/// <param name="setting">The setting.</param>
		/// <param name="value">The value.</param>
		public void Update(Setting setting, SettingUpdate value)
		{
			value.Validate();

			setting.Title = value.Title;
			setting.Group = value.Group;
			setting.Key = value.ItemKey;
			setting.Value = value.ItemValue;

			this.Update(setting);
		}

		/// <summary>
		/// Deletes the specified setting.
		/// </summary>
		/// <param name="setting">The <see cref="Setting"/>.</param>
		public void Delete(Setting setting)
		{
			this.SettingRepository.Delete(setting);
		}
	}
}